package Majisuka
{
	import mx.core.INavigatorContent;
	import mx.effects.IAbstractEffect;

	public class Group
	{
		private var _targetPlace:int ;
		private var _id:int ;
		private var _myPlaces:Vector.<int> ; // May not be the most updated version
		private var _rank:Vector.<int> ;
		private var _parent:Vector.<int> ; // Variable used to determine how to move the people
		
		public function Group()
		{
			_targetPlace = -1 ;
			_myPlaces = new Vector.<int> ;
			_rank = new Vector.<int> ;
		}

		public function get parent():Vector.<int>
		{
			return _parent;
		}

		public function set parent(value:Vector.<int>):void
		{
			_parent = value;
		}

		public function get myPlaces():Vector.<int>
		{
			return _myPlaces;
		}

		public function set myPlaces(value:Vector.<int>):void
		{
			_myPlaces = value;
		}

		public function get id():int
		{
			return _id;
		}

		public function set id(value:int):void
		{
			_id = value;
		}

		public function get targetPlace():int
		{
			return _targetPlace;
		}

		public function set targetPlace(value:int):void
		{
			_targetPlace = value;
		}
		
		public function FindTargetPlace( game:Game ):int
		{
			if ( _targetPlace >= 0 &&
				game.places[ _targetPlace ].owner != _id )
				return _targetPlace;
			
			//var myPlaces:Vector.<int> = new Vector.<int> ;
			var adjEnemyPlaces:Vector.<int> = new Vector.<int> ;
			var i:int ;
			var len:int ;
			var j:int ;
			var lenj:int ;
			var k:int ;
			
			len = _myPlaces.length ;
			for ( i = 0 ; i < len ; ++i )
				_myPlaces.pop() ;
			
			_targetPlace = -1 ;
			
			// Find my places first
			len = game.places.length ;
			for ( i = 0 ; i < len ; ++i )
			{
				if ( game.places[i].owner == _id )
					_myPlaces.push( i ) ;
			}
			
			len = _myPlaces.length ;
			for ( i = 0 ; i < len ; ++i )
			{
				lenj = game.places[ _myPlaces[i] ].neigh.length ;	
				for ( j = 0 ; j < lenj ; ++j )
				{
					k = game.places[ _myPlaces[i] ].neigh[j] ;
					if ( game.places[k].owner != _id  )
						adjEnemyPlaces.push( k ) ;
				}
			}
			
			// Determine the target. 
			// The place owned by the player has the priority to be attacked.
			len = adjEnemyPlaces.length ;
			for ( i = 0 ; i < len ; ++i )
			{
				if ( game.places[ adjEnemyPlaces[i] ].owner == game.player )
					return adjEnemyPlaces[i] ;
			}
			
			return adjEnemyPlaces[0] ; 
		}
		
		public function GetGroupMoveStrategy( to:int, g:Game ):Vector.<int>
		{
			var visited:Vector.<Boolean> = new Vector.<Boolean> ;
			_parent = new Vector.<int> ;
			var i:int ;
			var len:int ;
			
			len = g.places.length ;
			for ( i = 0 ; i < len ; ++i )
			{	
				visited.push( false ) ;
				parent.push( -1 ) ;
			}
			GetParent( to, -1, visited, g ) ;
			return _parent ;
		}
		
		private function GetParent( tag:int, pre:int, visited:Vector.<Boolean>, g:Game ):void
		{
			if ( visited[tag] == true )
				return ;
			
			var i:int ;
			var k:int
			var len:int ;
			_parent[tag] = pre ;
			visited[tag] = true ;
			len = g.places[tag].neigh.length ;
			for ( i = 0 ; i < len ; ++i )
			{
				k = g.places[tag].neigh[i] ;
				if ( g.places[k].owner == _id )
					GetParent( k, tag, visited, g ) ;
			}
		}
	}
}